package com.app_library.utils;

/**
 * 作者 : hjb
 * 时间 : 2016/4/27.
 */
public class JsonFormatTool {
    private static String SPACE = "   ";

    public JsonFormatTool() {
    }

    public static String formatJson(String json) {
        StringBuffer result = new StringBuffer();
        int length = json.length();
        int number = 0;
        boolean key = false;

        for(int i = 0; i < length; ++i) {
            char var6 = json.charAt(i);
            if(var6 != 91 && var6 != 123) {
                if(var6 != 93 && var6 != 125) {
                    if(var6 == 44) {
                        result.append(var6);
                        result.append('\n');
                        result.append(indent(number));
                    } else {
                        result.append(var6);
                    }
                } else {
                    result.append('\n');
                    --number;
                    result.append(indent(number));
                    result.append(var6);
                    if(i + 1 < length && json.charAt(i + 1) != 44) {
                        result.append('\n');
                    }
                }
            } else {
                if(i - 1 > 0 && json.charAt(i - 1) == 58) {
                    result.append('\n');
                    result.append(indent(number));
                }

                result.append(var6);
                result.append('\n');
                ++number;
                result.append(indent(number));
            }
        }

        return result.toString();
    }

    private static String indent(int number) {
        StringBuffer result = new StringBuffer();

        for(int i = 0; i < number; ++i) {
            result.append(SPACE);
        }

        return result.toString();
    }
}
